let app = getApp()
const Controller = require('../controllers/controllers.js').controller;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressShow: false,
        topNavigationHeight: 0,
        ageShow: false,
        region: [],
        formData: {
            name: '',
            age: '',
            sex: '',
            job: '',
            province: "",
            city: "",
            area: "",
            hasPromoterInfo: "",
            hasExperience: "",
            mobile: "",
            email: ""
        },
        isShow: false,
        hasExperienceList: ['是', '否'],
        sexs: ['男', '女'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    //提交申请
    submit() {
        let formData = this.data.formData
        formData.sex = formData.sex ==='' ? '' : formData.sex === '男' ? 1 : 2
        formData.hasExperience = formData.hasExperience ==='' ? '' : formData.hasExperience === '是' ? 1 : 0
          // 空项拦截
          if(!this.format(formData)){
            return
        }
        Controller.approvePromoter(formData).then(res => {
            this.setData({
                isShow: true
            })
        })
    },

    goBack() {
        wx.navigateBack({
            delta: 1
        })
    },

    format(option){
   console.log('format',option)
     for(let key in option){
         
         if(!option[key]&&option[key]!==0){
         wx.showToast({
           title: '内容填写不能为空',
           icon:'none'
         })
          return false
         }
     }
     return true

    },
    
    //Picker改变事件
    bindRegionChange(e) {
        let region = e.detail.value
        let formData = this.data.formData
        formData.province = region[0]
        formData.city = region[1]
        formData.area = region[2]

    
      
        this.setData({
            region,
            formData
        })
    },

    // 输入触发
    change(e) {
        let type = e.target.dataset.type
        let value = e.detail.value
        let that = this

        if (!type) {
            return
        }

        switch (type) {
            case 'name':
                that.setData({
                    'formData.name': value
                })
                break;
            case 'job':
                that.setData({
                    'formData.job': value
                })
                break;
            case 'age':
                that.setData({
                    'formData.age': value
                })
                break;
            case 'hasPromoterInfo':
                that.setData({
                    'formData.hasPromoterInfo': value
                })
                break;
            case 'mobile':
                that.setData({
                    'formData.mobile': value
                })
                break;
            case 'email':
                that.setData({
                    'formData.email': value
                })
                break;
            default:


        }



    },
    // 选择是否有经验
    selectHasExperienceList(e) {

        let value = e.detail.value
        this.setData({
            'formData.hasExperience': value
        }, () => {
            this.closePopup()
        })

    },
    selectSex(e) {
        let value = e.detail.value
        this.setData({
            'formData.sex': value
        }, () => {
            this.closePopup()
        })
    },
    // 打开弹窗
    openPopup(e) {
        let type = e.currentTarget.dataset.type
        if (!type) {
            return
        }
        this.setData({
            show: true,
            type
        })
    },
    // 关闭弹窗
    closePopup(e) {
        this.setData({
            show: false
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.setData({
            topNavigationHeight: app.globalData.topNavigationHeight + 'rpx'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})